import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.swing.JPanel;

/*
 * Take a look at the setters.  If any changes are made, the recovery file is updated immediately.
 * 
 * This may seem redundant with respect to the Model class, but I think this a cleaner, easier to work with way of saving to files.
 * 
 * Separation of concerns will make your life that much easier.
 */

public class Game implements Serializable {
	
	// Zero will always represent player 1, and 1 will represent player 2.
	
	User[] users;


	boolean isFlyingEnabled;
	int [][] boardLayout = { {0,0,0,0,0,0,0,0,0}, {0,0,3,3,0,3,3,0,0}, {0,3,0,3,0,3,0,3,0}, {0,3,3,0,0,0,3,3,0}, {0,0,0,0,3,0,0,0,0}, {0,3,3,0,0,0,3,3,0}, {0,3,0,3,0,3,0,3,0}, {0,0,3,3,0,3,3,0,0}, {0,0,0,0,0,0,0,0,0}, };;
	int playersTurn;
	int[] tokensLeft = new int[2];
	int[] tournamentStatistics = new int[2];
	boolean isTournamentModeEnabled;
	int token1Played = 0;
	int token2Played = 0;
	
	
	
	
	
	public Game(User[] users, boolean isFlyingEnabled){
		this.users = users;
		this.isFlyingEnabled = isFlyingEnabled;
		//boardLayout = new int[8][8];
		//boardLayout = { {3,3,3,3,3,3,3,3,3}, {3,0,3,3,0,3,3,0,3}, {3,3,0,3,0,3,0,3,3}, {3,3,3,0,0,0,3,3,3}, {3,0,0,0,3,0,0,0,3}, {3,3,3,0,0,0,3,3,3}, {3,3,0,3,0,3,0,3,3}, {3,0,3,3,0,3,3,0,3}, {3,3,3,3,3,3,3,3,3}, }; 





		playersTurn = 0;
		tokensLeft[0] = 9;
		tokensLeft[1] = 9;
		tournamentStatistics[0] = 0;
		tournamentStatistics[1] = 0;
		isTournamentModeEnabled = false;
	}
	
	public void updateRecoveryFile() throws IOException{
		// Retrieving & formatting the date.
		Locale locale = new Locale("US");
		Date today;
		String result;
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("MM_dd_yy", locale);
		today = new Date();
		result = formatter.format(today);
		
		// Writing to the file.
		FileOutputStream fos = new FileOutputStream(getUsers()[0].getName() + "_" + getUsers()[1].getName() + "_" + result + ".recovery");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(this);
		oos.close();
	}
	
	
	
	// Getters & Setters
	public User[] getUsers() {
		return users;
	}

	public int getToken1Played()
	{
		return token1Played;
	}
	public int getToken2Played()
	{
		return token2Played;
	}
	public void setToken1Played(int token1Played)
	{
		this.token1Played = token1Played;
	}
	public void setToken2Played(int token2Played)
	{
		this.token2Played = token2Played;
	}
	
	public void setUsers(User[] users) throws IOException {
		this.users = users;
		updateRecoveryFile();
	}

	public boolean isFlyingEnabled() {
		return isFlyingEnabled;
	}

	public void setFlyingEnabled(boolean isFlyingEnabled) throws IOException {
		this.isFlyingEnabled = isFlyingEnabled;
		//updateRecoveryFile();
	}

	public int[][] getBoardLayout() {
		return boardLayout;
	}

	public void setBoardLayout(int[][] boardLayout) throws IOException {
		this.boardLayout = boardLayout;
		//updateRecoveryFile();
	}

	public int getPlayersTurn() {
		return playersTurn;
	}

	public void setPlayersTurn(int playersTurn) throws IOException {
		this.playersTurn = playersTurn;
		//updateRecoveryFile();
	}

	public int[] getTokensLeft() {
		return tokensLeft;
	}

	public void setTokensLeft(int[] tokensLeft) throws IOException {
		this.tokensLeft = tokensLeft;
		//updateRecoveryFile();
	}

	public int[] getTournamentStatistics() {
		return tournamentStatistics;
	}

	public void setTournamentStatistics(int[] tournamentStatistics) throws IOException {
		this.tournamentStatistics = tournamentStatistics;
		//updateRecoveryFile();
	}

	public boolean isTournamentModeEnabled() {
		return isTournamentModeEnabled;
	}

	public void setTournamentModeEnabled(boolean isTournamentModeEnabled) throws IOException {
		this.isTournamentModeEnabled = isTournamentModeEnabled;
		//updateRecoveryFile();
	}
}
